package com.bps.zzant.controller;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.bps.zzant.model.OrderGarment;
import com.bps.zzant.services.OrderService;

@Controller
public class PublicController {
	
private static final Logger logger = LoggerFactory.getLogger(PublicController.class);
	@Autowired
	private OrderService orderService;

	@RequestMapping(value = "/publicvieworder/{customerId}/{orderId}", method = RequestMethod.GET)
	public ModelAndView viewCustomerOrder(@PathVariable int customerId, @PathVariable int orderId, HttpServletRequest request) {
		logger.info("----- PublicController.viewCustomerOrder() --------");
		
		OrderGarment order = orderService.getOrderById(orderId);
		Map<String, Object> map = new HashMap<String, Object>();
		//if order not exists or order's customer id does not match the customer id, then show the not found page
		logger.info("-------item size:    " + order.getGarmentItems().size());
		
		if(order==null || customerId != order.getCustomerId()){
			return new ModelAndView("viewordererror", map);
		}
		// model.addAttribute("serverTime", formattedDate );
		map.put("order", order);
		
		return new ModelAndView("vieworder", map);
	}

	@RequestMapping(value = "/publicprintorder/{customerId}/{orderId}", method = RequestMethod.GET)
	public ModelAndView printOrderPdf(	@PathVariable int customerId, @PathVariable int orderId, HttpServletRequest request,	HttpServletResponse response)throws Exception{

		OrderGarment order = orderService.getOrderById(orderId);
		Map map = new HashMap();
		
		if(order==null || customerId != order.getCustomerId()){
			return new ModelAndView("viewordererror", map);
		}
		
		map.put("format", "pdf");
		map.put("order_id", orderId+"");

		return new ModelAndView("orderPdfView", map);
		}
}
